package com.example.demo.demos.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;

/**
 * 计算文件是否相同
 */
public class FileValidator {
    public static boolean compareFiles(Path file1, Path file2) throws IOException {
        byte[] bytes1 = Files.readAllBytes(file1);
        byte[] bytes2 = Files.readAllBytes(file2);
        return MessageDigest.isEqual(bytes1, bytes2);
    }

    public static void main(String[] args) {
        try {
            // 1. 准备要比较的文件路径
            Path file1 = Paths.get("D:\\ProjectSpace\\person_manage\\src\\main\\resources\\db\\person.db");
            Path file2 = Paths.get("D:\\BrowserDownload\\person.db");

            // 2. 调用比较方法
            boolean isSame = compareFiles(file1, file2);

            // 3. 输出比较结果
            System.out.println("文件是否相同: " + isSame);
        } catch (IOException e) {
            System.err.println("文件比较出错: " + e.getMessage());
        }
    }
}